<?php

class KingdomController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
    }

    public function indexAction()
    {
        // action body
    }

    public function listAction()
    {
    	$auth = Zend_Auth::getInstance();
    	$buildingsArray = new ArrayIterator();
    	$userBuildings = new Application_Model_UserBuildings();
    	$buildings = new Application_Model_BuildingsTbl();
        if ($auth->hasIdentity()) 
        {
            $username = $auth->getIdentity()->username; 
            $user_id = $auth->getIdentity()->id; 
            
            $roundSettings = new Application_Model_RoundSettingsTbl();
            $result = $roundSettings->checkIfClassAndRaceAreSelected($user_id);
            if($result == false)
            	$this->_redirect('authentication/login');
            //set default values for all buildings
        	$result = $buildings->fetchAll();
            if($result != null)
            {
        		foreach($result as $value) 
        		{
        			$key = $value['name'];
           			$buildingsArray[$key] = 0;
        		}
            }
            	
            //set lvl for each building individually
            $result = $userBuildings->getUserBuildings($user_id);
            if($result != null)
            {
            	foreach($result as $value) 
        		{
        			$key = $value['name'];
        			$val = $value['building_lvl'];
           			$buildingsArray[$key] = $val;
        		}
            }
          
            
            $this->view->buildingsArray = $buildingsArray;
        }
     	else {
        	$this->_redirect('authentication/login');	
        }
    }

}









